var express = require('express');
var router = express.Router();
const { getList, getDetail, register, editUser, deleteUser, login } = require('../controller/user')
const { SuccessModel, ErrorModel } = require('../model/resModel')
const { currentTime } = require('../util/currentTime')

router.get('/list', function(req, res, next) {
    const keyword = req.query.keyword
    const promise = getList(keyword)
    promise.then((sqlData) => {
        res.json(
            new SuccessModel(sqlData, '用户列表')
        )
    })
});

router.get('/detail', function(req, res, next) {
    const id = req.query.id
    const promise = getDetail(id)
    promise.then((sqlData) => {
        if (sqlData.length > 0) {
            res.json(
                new SuccessModel(sqlData[0], '用户详情')
            )
        } else {
            res.json(
                new ErrorModel({
                    tip: '未查询到相关用户',
                    failTime: currentTime()
                }, '用户详情')
            )
        }
    })
});

router.post('/register', function(req, res, next) {
    const username = req.body.username
    const realname = req.body.realname
    const password = req.body.password
    const promise = register(username, realname, password, currentTime(), currentTime())
    promise.then((sqlData) => {
        if (sqlData.insertId && sqlData.affectedRows) {
            res.json(
                new SuccessModel({
                    tip: '注册成功',
                    createTime: currentTime()
                }, '注册用户')
            )
        } else {
            res.json(
                new ErrorModel({
                    tip: '注册失败',
                    failTime: currentTime()
                }, '注册用户')
            )
        }  
    })
});

router.post('/edit', function(req, res, next) {
    const username = req.body.username
    const realname = req.body.realname
    const password = req.body.password
    const id = req.query.id
    const promise = editUser(username, realname, password, currentTime(), id)
    promise.then((sqlData) => {
        if (sqlData.affectedRows) {
            res.json(
                new SuccessModel({
                    tip: '编辑成功',
                    updateTime: currentTime()
                }, '编辑用户')
            )
        } else {
            res.json(
                new ErrorModel({
                    tip: '编辑失败',
                    failTime: currentTime()
                }, '编辑用户')
            )
        }  
    })
});

router.post('/delete', function(req, res, next) {
    const id = req.query.id
    const promise = deleteUser(id)
    promise.then((sqlData) => {
        if (sqlData.affectedRows) {
            res.json(
                new SuccessModel({
                    tip: '删除成功',
                    updateTime: currentTime()
                }, '删除用户')
            )
        } else {
            res.json(
                new ErrorModel({
                    tip: '删除失败',
                    failTime: currentTime()
                }, '删除用户')
            )
        }  
    })
});

router.post('/login', function(req, res, next) {
    const username = req.body.username
    const password = req.body.password
    const promise = login(username, password)
    promise.then((sqlData) => {
        if (sqlData.length > 0) {
            req.session.username = username
            req.session.password = password
            res.json(
                new SuccessModel({
                    tip: '登录成功',
                    updateTime: currentTime()
                }, '用户登录')
            )
        } else {
            res.json(
                new ErrorModel({
                    tip: '登录失败',
                    failTime: currentTime()
                }, '用户登录')
            )
        }  
    })
});

router.get('/login', function(req, res, next) {
    const username = req.query.username
    const password = req.query.password
    const promise = login(username, password)
    promise.then((sqlData) => {
        req.session.username = username
        req.session.password = password
        if (sqlData.length > 0) {
            res.json(
                new SuccessModel({
                    tip: '登录成功',
                    updateTime: currentTime()
                }, '用户登录')
            )
        } else {
            res.json(
                new ErrorModel({
                    tip: '登录失败',
                    failTime: currentTime()
                }, '用户登录')
            )
        }  
    })
});

router.get('/login-test', (req, res, next) => {
    if (req.session && req.session.username) {
        res.json(
            new SuccessModel({
                tip: '已登录',
                loginTime: currentTime()
            })
        )
    } else {
        res.json(
            new ErrorModel({
                tip: '未登录',
                failTime: currentTime()
            })
        )
    }
})

module.exports = router;
